<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="Title" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.label" placeholder="Label" clearable style="width: 180px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <DictGroupInput ref="dictGroupInput" :value.sync="listQuery.category" dict-group="bookmark_category" class="filter-item" placeholder="Category" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">Search</el-button>
      <!-- show column popover -->
      <el-popover placement="bottom" width="150" trigger="click">
        <el-checkbox v-model="showRows.category" class="filter-item" @change="tableKey = tableKey + 1">category</el-checkbox>
        <el-checkbox v-model="showRows.label" class="filter-item" @change="tableKey = tableKey + 1">label</el-checkbox>
        <el-checkbox v-model="showRows.subtitle" class="filter-item" @change="tableKey = tableKey + 1">subtitle</el-checkbox>
        <el-checkbox v-model="showRows.description" class="filter-item" @change="tableKey = tableKey + 1">description</el-checkbox>

        <el-button slot="reference" v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-setting" />
      </el-popover>

      <router-link :to="{name:'CreateBookmark'}">
        <el-button v-waves class="filter-item" type="primary" plain icon="el-icon-plus" />
      </router-link>
    </div>

    <el-table :key="tableKey" v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" :default-sort="{prop:'id',order:'descending'}" @sort-change="sortChange">
      <el-table-column align="center" prop="id" label="ID" width="80" sortable="custom" />
      <el-table-column header-align="center" prop="title" label="Title" show-overflow-tooltip />
      <el-table-column v-if="showRows.subtitle" header-align="center" prop="subtitle" label="Subtitle" show-overflow-tooltip />
      <el-table-column align="center" prop="category" label="Category" />
      <el-table-column v-if="showRows.label" header-align="center" prop="label" label="Label" show-overflow-tooltip />
      <el-table-column header-align="center" label="Url" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link :href="scope.row.url" type="primary" target="_blank">{{ scope.row.url }}</el-link>
        </template>
      </el-table-column>
      <el-table-column v-if="showRows.description" header-align="center" prop="description" label="Description" show-overflow-tooltip />

      <el-table-column width="180px" align="center" prop="modify_at" sortable="custom" label="UpdateTime">
        <template slot-scope="scope">
          <span>{{ scope.row.modifyAt | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="Actions" width="170">
        <template slot-scope="scope">
          <router-link :to="{name: 'EditBookmark', params: scope.row }">
            <el-button type="primary" size="mini" icon="el-icon-edit" plain />
          </router-link>
          <el-button v-clipboard:copy="scope.row.url" v-clipboard:success="clipboardSuccess" type="primary" size="mini" icon="el-icon-document-copy" plain />
          <el-button type="danger" size="mini" icon="el-icon-delete" plain @click="handleDelete(scope.row.id)" />
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { fetchList, deleteBookmark } from '@/api/info/bookmark'
import { getSorts } from '@/utils/index'
import waves from '@/directive/waves' // waves directive
import clipboard from '@/directive/clipboard/index.js' // use clipboard by v-directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import DictGroupInput from '@/views/components/DictGroupInput'

export default {
  name: 'BookmarkList',
  components: { Pagination, DictGroupInput },
  directives: { waves, clipboard },
  data() {
    return {
      tableKey: 0,
      showRows: {
        category: true,
        label: true,
        subtitle: false,
        description: true
      },
      list: null,
      total: 0,
      sort: {},
      listLoading: true,
      listQuery: {
        category: undefined,
        label: undefined,
        title: undefined,
        uid: 0,
        current: 1,
        size: 10,
        asc: undefined,
        desc: ['id']
      }
    }
  },
  computed: {
    ...mapGetters(['uid', 'dictionaries']),
    categories() {
      return this.dictionaries.filter(label => label.dictGroup === 'bookmark_category')
    }
  },
  created() {
    this.listQuery.uid = this.uid
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      console.log(this.listQuery)
      fetchList(this.listQuery)
        .then(response => {
          this.list = response.result.list
          this.total = response.result.total
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.current = 1
      this.getList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.sort[prop] = undefined
      if (order) {
        this.sort[prop] = order !== 'descending'
      }
      const sort = getSorts(this.sort)
      this.listQuery.asc = sort.asc
      this.listQuery.desc = sort.desc
      this.handleFilter()
    },
    handleDelete(id) {
      this.$confirm('Confirm delete record?', 'Warning', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(() => {
        deleteBookmark(id).then(response => {
          this.$message({
            type: 'success',
            message: 'deleted successfully!'
          })
          this.getList()
        })
      })
    },
    clipboardSuccess() {
      this.$message({
        message: 'Copy successfully',
        type: 'success',
        duration: 1500
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.royal-blue {
  color: RoyalBlue;
}
</style>
